package com.farm.controller.advice;

import com.farm.result.Result;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * MVCExceptionHandler
 *
 * @author ctidy
 * @since 2021/1/7
 */
@RestControllerAdvice
public class MVCExceptionHandler {

    @ExceptionHandler(UnauthorizedException.class)
    public Result onUnauthorizedException(UnauthorizedException e) {
        return Result.unauthorized("权限不足");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public Result onIncorrectCredentialsException(IncorrectCredentialsException e) {
        return Result.build(Result.ResultCode.INCORRECT_CREDENTIALS);
    }

    @ExceptionHandler(LockedAccountException.class)
    public Result onLockedAccountException(LockedAccountException e) {
        return Result.build(Result.ResultCode.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(UnknownAccountException.class)
    public Result onUnknownAccountException(UnknownAccountException e) {
        return Result.build(Result.ResultCode.UNKNOWN_ACCOUNT);
    }

}
